package com.runshu.runshuwulian.ui.activity;

import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.hyphenate.easeui.mwidget.runtimepermission.PermissionsManager;
import com.runshu.runshuwulian.R;
import com.runshu.runshuwulian.base.BaseActivity;

import java.util.Timer;
import java.util.TimerTask;

import io.reactivex.functions.Consumer;

public class SplashActivity extends BaseActivity {

    @Override
    protected int setContentViewResId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView( savedInstanceState );

    }

    @Override
    protected void onStart() {
        super.onStart();
        requestPermissions();
    }

    private void requestPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PermissionsManager.getInstance().requestAllManifestPermissionsByRxPermission( this, new Consumer<Boolean>() {
                @Override
                public void accept(Boolean aBoolean) throws Exception {
                    if (aBoolean) goThread();
                    else showShortToast( "请到系统设置中允许相关权限" );
                }
            } );
        } else {
            goThread();
        }
    }

    long timeConut = 0;
    Timer timer = new Timer();
    boolean isPause = false;

    @Override
    protected void onResume() {
        super.onResume();
        isPause = false;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isPause = true;
//        timeConut = 0;
    }

    private void goThread() {
        timer.schedule( new TimerTask() {
            @Override
            public void run() {
                if (isPause) return;
                timeConut++;

                if (timeConut > 3) {
                    imDataInit();

                    if (timer != null)
                        timer.cancel();

                }
            }
        }, 0, 1000 );
    }

    private void imDataInit() {
        long start = System.currentTimeMillis();
//        startActivityByAlphAnim( ScanGoodsActivity.class );
        startActivityByAlphAnim( LoginActivity.class );
        finish();
    }
}
